using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;
using System.Collections;
using System.IO.Packaging;

namespace LogAnalyzerIIS.LogClasses
{
	public class IISLogConverter : LogConverter
	{
		protected override XmlDocument CreateWorksheetXML(List<string> sharedStringsOutput, string inputFilePath)
		{
			// for searching, the string is what we search for so it is the key
			// the int is the index which gets returned from a search
			Dictionary<string, int> sharedStringsSearch = new Dictionary<string, int>();

			XmlElement sheetData = PrepareWorksheetXML(sharedStringsOutput, sharedStringsSearch);

			// read the input file
			StreamReader inputFileReader = new StreamReader(inputFilePath);
			int[] columnMappings = CreateIISColumnMappings();

			// Excel starts rows at index 1 so we add data at row 2 (Row 1 is the column headers)
			int currentExcelRowIndex = 2;

			while (!inputFileReader.EndOfStream)
			{
				// read a line of the input file
				string inputLine = inputFileReader.ReadLine().Trim();

				// check that the line has text
				if (inputLine.Length > 0)
				{
					// step 1, split the line up into fields
					string[] inputLineValues = inputLine.Split(new char[] { ',' });

					// step 2, add the to the output array using the order specified in the column mappings
					// (the output array is the same size and order of the output columns)
					string[] outputValues = new string[outputColumns.Count];
					for (int i = 0; i < outputColumns.Count; i++)
					{
						if (columnMappings[i] > -1)
							outputValues[i] = inputLineValues[columnMappings[i]];
						else
							outputValues[i] = "";
					}

					// write the row
					WriteRow(sheetData, currentExcelRowIndex, outputValues, sharedStringsSearch, sharedStringsOutput);

					// move on the next row
					currentExcelRowIndex++;
				}
			}

			inputFileReader.Close();

			// discard the shared strings search array, we dont need it anymore
			sharedStringsSearch.Clear();

			return worksheetXML;
		}

		/// <summary>
		///  Create the column mappings that apply to W3C Extended logs
		/// </summary>
		/// <returns>an array of integers</returns>
		private static int[] CreateIISColumnMappings()
		{
			// initialize the column mapping array
			int[] outputColumnMappings = new int[outputColumns.Count];
			for (int i = 0; i < outputColumns.Count; i++)
				outputColumnMappings[i] = -1;

			outputColumnMappings[0] = 2;    // date
			outputColumnMappings[1] = 3;    // time
			outputColumnMappings[2] = 0;    // client IP
			outputColumnMappings[3] = 1;    // user name
			outputColumnMappings[4] = 4;    // service and instance
			outputColumnMappings[5] = 5;    // server name
			outputColumnMappings[6] = 6;    // server IP
			outputColumnMappings[8] = 12;   // request type
			outputColumnMappings[9] = 13;   // target of operation
			outputColumnMappings[10] = 14;  // parameters
			outputColumnMappings[11] = 10;  // service status code
			outputColumnMappings[12] = 11;  // windows status code
			outputColumnMappings[13] = 8;   // client bytes sent
			outputColumnMappings[14] = 9;   // server bytes sent
			outputColumnMappings[15] = 7;   // time taken

			return outputColumnMappings;
		}
	}
}
